#include "windowmover.h"

WindowMover::WindowMover(QWidget *aWidget, QString aPosVar){
    moveable = aWidget;
    isMoving = false;
    posVar = aPosVar;
}

bool WindowMover::eventFilter(QObject *object, QEvent *event){
    if(!isMoving && event->type()==QEvent::MouseButtonPress){
        QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
        if(mouseEvent->modifiers()==Qt::NoModifier &&
           mouseEvent->button()==Qt::LeftButton){
            isMoving = true;
            start = mouseEvent->globalPos();
            return true;
        }
    }
    if(isMoving && event->type()==QEvent::MouseMove){
        QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
        moveable->move(moveable->pos()+mouseEvent->globalPos()-start);
        start = mouseEvent->globalPos();
        return true;
    }
    if(isMoving && event->type()==QEvent::MouseButtonRelease){
        isMoving = false;
        moveable->setFocus(Qt::ActiveWindowFocusReason);
        return true;
    }

    if(event->type()==QEvent::Move){
        QMoveEvent *moveEvent = dynamic_cast<QMoveEvent*>(event);
        QSettings settings;
//        qDebug() << "Pos: " << moveEvent->pos().x() << ", " << moveEvent->pos().y();
        settings.setValue(posVar+"_X", moveEvent->pos().x());
        settings.setValue(posVar+"_Y", moveEvent->pos().y());
    }
    return false;
}
